package dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.HandlerConstant;
import custom.MyDialog;
import customhandler.MyHandler;
import singleton.SingletonClass;

/**
 * Created by Administrator on 2018/4/20.
 */

public class DateDialog extends MyDialog {

    @BindView(R.id.tv_dialog_title)
    TextView tvTitle;
    @BindView(R.id.dp_dialog_date)
    DatePicker datePicker;
    private String title;
    private String date;
    private OnClickListener onClickListener = null;
    private MyHandler allHnadler = SingletonClass.getSingleton(MyHandler.class);

    public DateDialog(@NonNull Context context, int themeResId, String title, String data, OnClickListener onClickListener) {
        super(context, themeResId);
        this.title = title;
        this.onClickListener = onClickListener;
        this.date = data;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date);
        ButterKnife.bind(this);

//        allHnadler.sendEmptyMessage(HandlerConstant.HIDE_BOTTOM_UI_MENU);

        tvTitle.setText(title);
        if (!date.contains("时间") && date.length() != 0) {
            int year = Integer.parseInt(date.substring(0, 4));
            int month = Integer.parseInt(date.substring(5, 7)) - 1;
            int day = Integer.parseInt(date.substring(8, 10));
            datePicker.init(year, month, day, null);
        }

    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
    }

    @OnClick({R.id.btn_dialog_date_cancle, R.id.btn_dialog_date_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_dialog_date_cancle:
                this.cancel();
                break;
            case R.id.btn_dialog_date_sure:
                StringBuffer sb = new StringBuffer();
                sb.append(String.format("%d-%02d-%02d",
                        datePicker.getYear(),
                        datePicker.getMonth() + 1,
                        datePicker.getDayOfMonth()));
                onClickListener.onClick(this, sb.toString());
                break;
        }
    }

    public interface OnClickListener {
        void onClick(Dialog dialog, String date);
    }
}
